//
//  ZSTabBarController.swift
//  ZSWeibo
//
//  Created by zsj1992 on 16/4/6.
//  Copyright © 2016年 ichange. All rights reserved.
//

import UIKit

class ZSTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tabBar = ZSTabBar()
        
        setValue(tabBar, forKey: "tabBar")
        
        tabBar.btnPlus.addTarget(self, action: "btnPlusDidClick", forControlEvents: .TouchUpInside)
        
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(ZSHomeController(), title: "首页", imageName: "tabbar_home",tag: 1000)
        addChildViewController(ZSMessageController(), title: "消息", imageName: "tabbar_message_center",tag: 1001)
        addChildViewController(ZSDiscoveryController(), title: "发现", imageName: "tabbar_discover",tag: 1002)
        addChildViewController(ZSProfileController(), title: "我的", imageName: "tabbar_profile",tag: 1003)
    }
   

    /**
     为什么每次都需要新创建一个view???
     */
    func btnPlusDidClick(){
    print("点击了发布按钮")
    //弹出发布界面的控制器
    //发布界面的view显示在这个弹出的控制器上面
    let c = ZSComposeView()
        c.show(self)
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    func addChildViewController(childController: UIViewController,title : String,imageName : String,tag:Int) {

        
        childController.tabBarItem = ZSTabBarItem()
        
        childController.title = title
        
//        childController.tabBarItem.title = title

        
        childController.tabBarItem.image = UIImage(named: imageName)
        
        childController.tabBarItem.selectedImage = UIImage(named: imageName+"_selected")
        
        childController.tabBarItem.tag = tag
        
        let nav = ZSNavicationController.init(rootViewController: childController)

        addChildViewController(nav)
    }
    
    
    
    //选中某个item的代理方法
//    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
//        print(item)
//        
//        print(item.tag)
//        
//        
//        //遍历tabBar的子控件
//        var index = 0
//        
//        for subView in tabBar.subviews{
//        
//            //如果是tabbarButton
//            if subView.isKindOfClass(NSClassFromString("UITabBarButton"){
//            
//            if index == item.tag-1000{
//            
//            //遍历item中的子空间
//            for v in subView.subviews{
//            
//            if v.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
//            
//            v.transform = CGAffineTransformMakeScale(0.5, 0.5)
//            UIView.animateWithDuration(0.25, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 2, options: [], animations: { () -> Void in
//            //先缩小 再放大
//            v.transform = CGAffineTransformMakeScale(1, 1)
//            
//            }, completion: { (_) -> Void in
//            print("OK")
//           
//                        })
//                    }
//                }
//            }
//        }
//    }
//        index++
//    }
//    
    
    
    
    
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        //        print(item.tag)
        var index = 0
        //一旦要是实现的 要学会使用视图层次查看器 来查找子视图
        for subView in tabBar.subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                if index == item.tag - 1000 {
                    //可以定位到点击的 UITabBarButton
                    //遍历UITabBarButton子视图
                    for v in subView.subviews {
                        if v.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            //                            print(v)
                            //执行动画效果
                            v.transform = CGAffineTransformMakeScale(0.5, 0.5)
                            UIView.animateWithDuration(0.25, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 2, options: [], animations: { () -> Void in
                                //先缩小 再放大
                                v.transform = CGAffineTransformMakeScale(1, 1)
                                
                                }, completion: { (_) -> Void in
                                    print("OK")
                            })
                        }
                    }
                }
                index++
            }
        }
    }

    
    
}
